package xps.and.uudaijia.userclient.view.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.socialize.utils.Log;
import com.zhy.autolayout.AutoFrameLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import xps.and.uudaijia.userclient.Constants;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.WXParmas;
import xps.and.uudaijia.userclient.data.baen.AliPrePayOrder;
import xps.and.uudaijia.userclient.data.baen.InformationBean;
import xps.and.uudaijia.userclient.data.baen.PrePayOrder;
import xps.and.uudaijia.userclient.data.net.UserNetWorks;
import xps.and.uudaijia.userclient.util.APay_v2;
import xps.and.uudaijia.userclient.view.base.BaseActivity;

/**
 * Created by Administrator on 2017/7/27.
 */

public class RechargeActivity extends BaseActivity {
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.balance_Text)
    TextView balanceText;
    @BindView(R.id.reflect_EditText)
    EditText reflectEditText;
    @BindView(R.id.reflect_Button)
    Button reflectButton;
    @BindView(R.id.WX_Layout)
    AutoFrameLayout WXLayout;
    @BindView(R.id.ZFB_Layout)
    AutoFrameLayout ZFBLayout;
    @BindView(R.id.actionbar_tv_right)
    TextView actionbarTvRight;
    @BindView(R.id.v_gap_line)
    View vGapLine;
    @BindView(R.id.actionbar_al)
    AutoRelativeLayout actionbarAl;
    @BindView(R.id.tv_100)
    TextView tv100;
    @BindView(R.id.tv_200)
    TextView tv200;
    @BindView(R.id.tv_300)
    TextView tv300;
    @BindView(R.id.tv_500)
    TextView tv500;

    private IWXAPI api;
    PrePayOrder.ReturnBodyBean responseBody;
    private String money = ""; //当前选择的金额
    String prePayId = "";
    String zfbID;

    @Override
    protected int getLayout() {
        return R.layout.activity_recharge;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

    @Override
    protected void setData() {
        Log.e("RechargeActivity", "setData");
    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
    }

    @OnClick({R.id.actionbar_iv_back, R.id.WX_Layout, R.id.ZFB_Layout,R.id.tv_100, R.id.tv_200, R.id.tv_300, R.id.tv_500})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.WX_Layout:
                if(!TextUtils.isEmpty(reflectEditText.getText().toString())) {
                    weCount();
                }else {
                    JUtils.Toast("请输入充值金额");
                }
                break;
            case R.id.ZFB_Layout:
                    if(!TextUtils.isEmpty(reflectEditText.getText().toString())) {
                        ZFBCount();
                    }else {
                        JUtils.Toast("请输入充值金额");
                    }
                break;
            case R.id.tv_100:
                money = "100";
                reflectEditText.setText("100");
                resetTextLable();
                tv100.setTextColor(getResources().getColor(R.color.blance_backround));
                break;
            case R.id.tv_200:
                money = "200";
                reflectEditText.setText("200");
                resetTextLable();
                tv200.setTextColor(getResources().getColor(R.color.blance_backround));
                break;
            case R.id.tv_300:
                money = "300";
                reflectEditText.setText("300");
                resetTextLable();
                tv300.setTextColor(getResources().getColor(R.color.blance_backround));
                break;
            case R.id.tv_500:
                money = "500";
                reflectEditText.setText("500");
                resetTextLable();
                tv500.setTextColor(getResources().getColor(R.color.blance_backround));
                break;
        }
    }

    void resetTextLable(){
        tv500.setTextColor(getResources().getColor(R.color.light_gray));
        tv300.setTextColor(getResources().getColor(R.color.light_gray));
        tv200.setTextColor(getResources().getColor(R.color.light_gray));
        tv100.setTextColor(getResources().getColor(R.color.light_gray));
        String sTemp = reflectEditText.getText().toString();
        if(!TextUtils.isEmpty(sTemp))
        reflectEditText.setSelection(reflectEditText.getText().length());
    }

    void ZFBCount() {
        UserNetWorks.getZFBCount("user", reflectEditText.getText().toString(), new Subscriber<AliPrePayOrder>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(AliPrePayOrder aliPrePayOrder) {
                if (aliPrePayOrder.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(aliPrePayOrder.getReturn_msg());
                } else if (aliPrePayOrder.getReturn_code().equals("SUCCESS")) {
                    zfbID = aliPrePayOrder.getReturn_body().getOrderString();
                    APay_v2.getInstance().setContext(RechargeActivity.this).doPay_v2(zfbID);
                }
            }
        });
    }

    void weCount() {
        UserNetWorks.getweCount("user", reflectEditText.getText().toString(), new Subscriber<PrePayOrder>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PrePayOrder prePayOrder) {
                if (prePayOrder.getReturn_code().equals("SUCCESS")) {
                    responseBody = prePayOrder.getReturn_body();
                    String prePayId = responseBody.getPrepayid();
                    doPay(new WXParmas(prePayId));
                }
            }
        });
    }

    void doPay(WXParmas wx) {
        PayReq payReq = new PayReq();
        payReq.appId = wx.appid;
        payReq.partnerId = wx.partnerid;
        payReq.prepayId = wx.prepayid;
        payReq.packageValue = "Sign=WXPay";
        payReq.nonceStr = wx.noncestr;
        payReq.timeStamp = wx.timestamp;
        payReq.sign = wx.sign;
        api.sendReq(payReq);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getInformation();
    }

    void getInformation() {
        UserNetWorks.getInformation("", new Subscriber<InformationBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(InformationBean informationBean) {
                InformationBean.ReturnBodyBean returnBodyBean = informationBean.getReturn_body();
                if (informationBean.getReturn_code().equals("SUCCESS")) {
                    balanceText.setText("￥" + returnBodyBean.getBalance());
                }
            }
        });
    }
}
